
xx <- c(-6,-3,-1,0,1,4,5,7,8,9)
yy <- c(5,8,9,10,10,9,7,2,1,-2)

points(xx, yy, col='blue', bg='blue', pch=21, cex=1.5)
grid()
beta <- (mean(xx*yy) - mean(xx)*mean(yy))/(mean(xx*xx) - (mean(xx))*(mean(xx)))
alpha <- mean(yy) - beta*mean(xx)

zz <- seq(-10,10,by=0.1)
bestfitline <- function(x) {
  alpha + beta*x
}
points(zz,bestfitline(zz),type='l',col='red', lwd=2)